package com.weixing.mall.provider.controller.front;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.StockCheckProductPerson;
import com.weixing.mall.provider.model.dto.StockCheckGoodsDto;
import com.weixing.mall.provider.model.dto.StockCheckNumDto;
import com.weixing.mall.provider.model.query.StockCheckQuery;
import com.weixing.mall.provider.service.IStockCheckProductPersonService;
import com.weixing.mall.provider.service.IStockCheckProductService;
import com.weixing.mall.provider.service.IStockCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 仓库前端控制
 * @ClassName FrontAppStockCheckController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/11/13
 * @Version 1.0
 **/
@RequestMapping("front/stockCheck")
@RestController
@Api(value = "前端：盘点列表Api",tags = "前端：盘点列表")
public class FrontAppStockCheckController extends BaseController {

    @Autowired
    private IStockCheckService stockCheckService;
    @Autowired
    private IStockCheckProductService stockCheckProductService;
    @Autowired
    private IStockCheckProductPersonService stockCheckProductPersonService;

    @GetMapping("")
    @ApiOperation(value = "列表查询")
    public Result list(StockCheckQuery stockCheckQuery){
        stockCheckQuery.setIsPublish(WhetherEnum.YES.getValue());//默认查询发布过的盘点单
        return ResultUtil.success(stockCheckService.selectList(initMpPage(),stockCheckQuery));
    }

    /**
     * 完成个人盘点
     * @param auth  当前登录用户
     * @param id    某一个商品完成盘点ID
     * @return
     */
    @GetMapping("/finish/{id}")
    @ApiOperation(value = "个人结束盘点")
    public Result finish(@SysInfo SysAuth auth, @PathVariable Long  id){
        StockCheckProductPerson entity=new StockCheckProductPerson();
        entity.setStockPerson(auth.getUid());
        entity.setIsFinish(WhetherEnum.YES.getValue());
        entity.setCheckProduct(id);
        stockCheckProductPersonService.finish(entity);
        return ResultUtil.success();
    }

    /**
     * 盘点商品详情
     * @param auth
     * @param checkId  盘点ID
     * @return
     */
    @GetMapping("specificDetails")
    @ApiOperation(value = "盘点详情(参数只需传入盘点单id，后台默认查询当前登陆人为盘点人)")
    public Result selectSpecificDetails(@SysInfo SysAuth auth, Long checkId){
        return ResultUtil.success(stockCheckService.selectSpecificDetailsReturnMap(initMpPage(),checkId,auth.getUid()));
    }

    /**
     * 盘点详情总数
     * @param auth
     * @param checkId  盘点单ID
     * @return
     */
    @GetMapping("specificDetailTotal")
    @ApiOperation(value = "盘点详情(参数只需传入盘点单id，后台默认查询当前登陆人为盘点人)")
    public Result specificDetailTotal(@SysInfo SysAuth auth, Long checkId){
        return ResultUtil.success(stockCheckService.specificDetailTotal(checkId,auth.getUid()));
    }

    /**
     *
     * @param auth
     * @param stockCheckGoodsDto
     * @return
     */
    @PostMapping("stockCheckGoods")
    @ApiOperation(value = "盘点商品(该方法认为盘点期间库存表没有发生改变 并且盘点人就是当前登录人， 若是有变化请重新写方法)")
    public Result stockCheckGoods(@SysInfo SysAuth auth,StockCheckGoodsDto stockCheckGoodsDto){
        stockCheckProductService.stockCheckGoods(stockCheckGoodsDto,auth.getUid());
        return ResultUtil.success();
    }

    /**
     * 二期不再执行修改功能
     * @param stockCheckNumDto
     * @return
     */
    @PostMapping("updateSpecificDetails")
    @ApiOperation(value = "盘点详情修改数量")
    @Deprecated
    public Result updateSpecificDetails(@RequestBody StockCheckNumDto stockCheckNumDto){
        stockCheckService.updateSpecificDetails(stockCheckNumDto);
        return ResultUtil.success();
    }

    /**
     * 删除某一个盘点列信息
     * @param stockCheckNumDto
     * @return
     */

    @PostMapping("delSpecificDetails")
    @ApiOperation(value = "删除某条盘点详情")

    public Result delSpecificDetails(@RequestBody StockCheckNumDto stockCheckNumDto){
        stockCheckService.delSpecificDetails(stockCheckNumDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "扫描盘点商品")
    @GetMapping("scanCheckGoods/{checkId}/{productSn}")
    public Result scanCheckGoods(@SysInfo SysAuth auth,@PathVariable(value = "checkId") Long checkId,@PathVariable(value = "productSn")String productSn){
        Boolean check=stockCheckProductService.checkProduct(checkId,productSn);
        if(check){
            return ResultUtil.error("该物品已经盘点完成，请勿重复操作！");
        }
        Boolean flag = stockCheckProductService.scanCheckProduct(checkId,productSn,auth);
        if(!flag){
            return ResultUtil.error("非此次盘点产品");
        }
        return ResultUtil.success();
    }
}
